<?php declare(strict_types=1);
namespace app\logic;

use app\exception\auth\AccountOrPasswordError;
use app\exception\AuthFailure;
use app\exception\ObjectHasExists;
use app\exception\ParamsInvalidate;
use app\exception\Success;
use app\execption\auth\TokenMust;
use app\library\DefaultConfig;
use app\facade\model\UserToken as FaUserTokenModel;
use app\facade\model\AuthGroup as FaAuthGroupModel;
use app\facade\model\AuthGroupAccess as FaAuthGroupAccessModel;
use app\model\UserToken;
use app\validate\User as UserValidate;
use clonelin\library\DateTime;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\Session;
use think\helper\Str;

class AuthGroup  extends Base {

    public function getAuthGroupList(){
        //
        $page = $this->request->param('page',DefaultConfig::CURRENT_PAGE);
        $limit = $this->request->param('limit',DefaultConfig::PAGE_SIZE);
        $list = FaAuthGroupModel::getAuthGroupList($page,$limit);
        $count = FaAuthGroupModel::getAuthGroupCount();

        $arr = [
            'count'=>$count,
            'list'=>$list
        ];
        return $arr;

    }


    public function del() : bool{
        $groupId = $this->request->param('group_id',0);
        Db::startTrans();
        $delGroup = FaAuthGroupModel::del($groupId);
        $delGroupAccess = FaAuthGroupAccessModel::del($groupId);
        if($delGroup && $delGroupAccess){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    public function haveAuth() : array {
        $groupId = $this->request->param('group_id',0);
        $authList = FaAuthGroupModel::haveAuth($groupId);
        // 只取值
        $authList = array_values($authList);
        return $authList;
    }


    public function update() : bool {
        //
        $groupData = $this->request->param();
        $update = FaAuthGroupModel::updateGroup($groupData);
        return $update;

    }

    public function create() : bool {
        //
        $groupData = $this->request->param();

        $isExistsName = FaAuthGroupModel::getGroupByName($groupData['name']);

        if($isExistsName){
            throw new ObjectHasExists('name has exists');
        }
        $update = FaAuthGroupModel::createGroup($groupData);
        return $update;

    }
}